var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    memberInfo: {},
    MyMenus: [{
        url: "/pages/ucenter/mypintuan/mypintuan",
        iconfont: "icon-pintuan",
        name: "我的拼团"
      },
      {
        url: "/pages/ucenter/mybargain/mybargain",
        iconfont: "icon-kanjia",
        name: "我的砍价"
      },
      {
        url: "/pages/ucenter/fenxiangyouli/fenxiangyouli",
        iconfont: "icon-fenxiang",
        name: "分享有礼"
      },
      {
        url: "/pages/ucenter/jifenduihuan/jifenduihuan",
        iconfont: "icon-ticket_integral",
        name: "积分兑换"
      },
      {
        url: "/pages/ucenter/address/address",
        iconfont: "icon-dizhi",
        name: "地址管理"
      },
      {
        url: "/pages/ucenter/commentfankui/commentfankui",
        iconfont: "icon-yijianfankui",
        name: "意见反馈"
      },
      {
        url: "/pages/about/about",
        iconfont: "icon-guanyuwomen",
        name: "关于我们"
      },
      {
        url: "/pages/ucenter/shoucang/shoucang",
        iconfont: "icon-noshoucang",
        name: "商品收藏"
      }
    ],
    couponCount: 0,
    memberUserid: '',
    orderinfo: 0
  },

  /**
   * 积分
   */
  gotojifen: function (e) {
    let token = wx.getStorageSync('token');
    if (token) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },

  /**
   * 优惠券
   */
  goPages: function (e) {
    let token = wx.getStorageSync('token');
    if (token) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  onShow: function () {
    let that = this;
    util.request(api.MemberCenter).then(function (res) {
      if (res.result == 1) {
        that.setData({
          couponCount: res.data.couponnum,
          memberInfo: res.data.meminfo,
          orderinfo: res.data.orderinfo,
        });
      }
    }).catch((err) => {
      reject(err);
    });;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  goLogin() {
    if (!this.data.memberInfo) {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },

  goBangding() {
    if (this.data.memberInfo) {
      var username = this.data.memberInfo.username;
      wx.navigateTo({
        url: "/pages/ucenter/login/bangding/bangding?username=" + username
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },

  goBrokerage() {
    if (this.data.memberInfo) {
      wx.navigateTo({
        url: "/pages/ucenter/brokerage/zhanghu/zhanghu"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },

  goOrder() {
    if (this.data.memberInfo) {
      wx.navigateTo({
        url: "/pages/ucenter/myorder/myorder"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },

  goOrderIndex(e) {
    if (this.data.memberInfo) {
      let ordertype = e.currentTarget.dataset.index
      let route = e.currentTarget.dataset.route
      wx.navigateTo({
        url: route + "?ordertype=" + ordertype,
        success: function (res) {},
        fail: function (res) {},
        complete: function (res) {},
      })
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },

  //退换/售后
  goOrderRefund() {
    if (this.data.memberInfo) {
      wx.navigateTo({
        url: "/pages/ucenter/myorderrefund/myorderrefund"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },

  //跳转至分销中心
  gofenxiaozhongxin() {
    let that = this;
    util.request(api.GoFenxiaozhongxin).then(function (res) {
      if (res.result == 1) {
        if (res.data) {
          wx.navigateTo({
            url: "/pages/ucenter/fenxiao/fenxiao/fenxiao"
          });
        } else {
          wx.navigateTo({
            url: "/pages/ucenter/fenxiao/fenxiaoshangshenqing/fenxiaoshangshenqing"
          });
        }
      } else {
        wx.showToast({
          title: '暂未开启分销功能！',
          icon: 'none'
        })
      }
    }).catch((err) => {
      reject(err);
    });
  }
})